//
//  MyDefaultAddressViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/11/23.
//  Copyright © 2017年 she1323. All rights reserved.
//  获取默认地址 并 保存

#import "YLMyDefaultAddressViewModel.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "WHProgressHUD.h"
#import "YLMyAdressModel.h"
@implementation YLMyDefaultAddressViewModel

- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetReceiveAddressDefault", YLURL];
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:nil success:^(id responseObject) {
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        YLMyAdressModel *model = [YLMyAdressModel yy_modelWithDictionary:responseObject[@"httpResult"]];
                        
                        YLAccount *account = [YLAccountTool account];
                        account.defaultAddress = model;
                        [YLAccountTool saveAccount:account];
                        [subscriber sendNext:nil];
                        
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                }];
                
                return nil;
            }];
        }];
    }
    return _requestCommand;
}

@end
